public class Binary_Search {
    public static int binarySearch(int[] arr, int key) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (arr[mid] == key) {
                return mid;
            }else if (arr[mid] > key){
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int key = 6;
        int[] arr = {1, 2, 3, 4, 5, 6};
        int ret = binarySearch(arr, key);
        System.out.println(ret);
    }
}
